<?php

namespace App\Models;

use App\Exceptions\ParamValidateFailedException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;

class Order extends Model
{
    use SoftDeletes;
    //Searchable;

    const
        //OPTION
        ORDER_TYPE_RUN = 0, //  跑腿
        ORDER_TYPE_ASK = 1, //  提问
        ORDER_TYPE_EDU = 2, //  教学
        ORDER_TYPE_DAILY = 3,   //  生活服务
        ORDER_TYPE_OTHER = 4,   //  其他
        ORDER_TYPE_ALL = 5, //  所有类型
        //STATUS
        ORDER_STATUS_PREPARED = 0,  //  未发布
        ORDER_STATUS_PUBLISHED = 1, //  已发布
        ORDER_STATUS_RUNNING = 2,   //  正在进行
        ORDER_STATUS_FINISHED = 3,  //  已完成
        ORDER_STATUS_COMMENT_DONE = 4,  //  已评论
        ORDER_STATUS_CANCELED = 5,  //  订单取消
        ORDER_STATUS_ALL = 6,       //所有状态
        //SORT
        ORDER_SORT_LATEST = 0,  //最近
        ORDER_SORT_PRICE = 1,   //从低到高
        ORDER_SORT_PRICE_DESC = 2,  //从高到低
        //SELECT
        MYORDER_ALL = 0,        //我发布或接收的订单
        MYORDER_PROVIDER = 1,   //我发布的订单
        MYORDER_RECEIVER = 2;   //我接收的订单

    //Elasticsearch 搜索

    /**
     * 索引名称
     *
     * @return string
     */
//    public function searchableAs()
//    {
//        return 'orders_index';
//    }

    /**
     * 可搜索的数据索引
     *
     * @return array
     */
//    public function toSearchableArray()
//    {
//        return [
//            'title' => $this->title,
//            'content' => $this->content,
//        ];
//    }


    //订单发布者
    public function providerUser(){
        return $this->hasOne(User::class,'openID','provider');
    }

    //订单接收者
    public function receiverUser(){
        return $this->hasOne(User::class,'openID','receiver');
    }

    //筛选订单类型
    public static function OrderType($order, $option){
        switch ($option){
            case Order::ORDER_TYPE_ALL:
                $order = $order->with('providerUser');
                break;
            case Order::ORDER_TYPE_RUN:
                $order = $order->where('type',Order::ORDER_TYPE_RUN)->with('providerUser');
                break;
            case Order::ORDER_TYPE_ASK:
                $order = $order->where('type',Order::ORDER_TYPE_ASK)->with('providerUser');
                break;
            case Order::ORDER_TYPE_EDU:
                $order = $order->where('type',Order::ORDER_TYPE_EDU)->with('providerUser');
                break;
            case Order::ORDER_TYPE_DAILY:
                $order = $order->where('type',Order::ORDER_TYPE_DAILY)->with('providerUser');
                break;
            case Order::ORDER_TYPE_OTHER:
                $order = $order->where('type',Order::ORDER_TYPE_OTHER)->with('providerUser');
                break;
            default:
                throw new ParamValidateFailedException();
        }
        return $order;
    }

    //筛选订单状态
    public static function OrderStatus($order, $status){
        switch ($status){
            case self::ORDER_STATUS_PREPARED:
                $order = $order->where('status',self::ORDER_STATUS_PREPARED);
                break;
            case self::ORDER_STATUS_PUBLISHED:
                $order = $order->where('status',self::ORDER_STATUS_PUBLISHED);
                break;
            case self::ORDER_STATUS_RUNNING:
                $order = $order->where('status',self::ORDER_STATUS_FINISHED);
                break;
            case self::ORDER_STATUS_COMMENT_DONE:
                $order = $order->where('status',self::ORDER_STATUS_COMMENT_DONE);
                break;
            case self::ORDER_STATUS_CANCELED:
                $order = $order->where('status',self::ORDER_STATUS_CANCELED);
                break;
            case self::ORDER_STATUS_ALL:
                break;
            default:
                throw new ParamValidateFailedException();
        }
        return $order;
    }


    //订单排序
    public static function OrderSort($order,$sort){
        switch ($sort){
            case self::ORDER_SORT_LATEST:
                $order = $order->latest();
                break;
            case self::ORDER_SORT_PRICE:
                $order = $order->orderBy('price');
                break;
            case self::ORDER_SORT_PRICE_DESC:
                $order = $order->orderBy('price','desc');
                break;
        }
        return $order;
    }

    //返回字符串类型
    public static function TypeToStr($orders){
        foreach ($orders as $order)
        {
            $arr = ['跑腿','提问','教学','生活服务','其他','所有类型'];
            $order->type = $arr[$order->type];
        }

    }
}
